package com.jiandaoshou.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2016/12/1.
 */

public class TimeUtil {
    public static TimeUtil mUtil;
    public static Calendar mCal;
    public static SimpleDateFormat format;
    public static SimpleDateFormat sdf;

    public static TimeUtil getInstance() {
        if (mUtil == null) {
            mUtil = new TimeUtil();
            mCal = Calendar.getInstance();
            format = new SimpleDateFormat("yyyy-MM-dd");
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
        return mUtil;
    }

    /**
     * 获取当前月的第一天
     */
    public String GetCurrentMonthFirstDay() {
        mCal.add(Calendar.MONTH,0);
        mCal.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        String firstDay = format.format(mCal.getTime());
        return firstDay;
    }

    /**
     * 获取当前月的最后一天
     */
    public String GetCurrentMonthLastDay() {
        mCal.set(Calendar.DAY_OF_MONTH, mCal.getActualMaximum(Calendar.DAY_OF_MONTH));
        String lastDay = format.format(mCal.getTime());
        return lastDay;
    }

    /**
     * 获取当天的开始时间
     */
    public String GetCurrentDayBeginTime() {
        return sdf.format(getStartTime());
    }

    /**
     * 获取当天的结束时间
     */
    public String GetCurrentDayEndTime() {
        return sdf.format(getEndTime());
    }

    public Date getStartTime() {
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        return todayStart.getTime();
    }

    public Date getEndTime() {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        return todayEnd.getTime();
    }
    /**获取过去的开始时间*/
    public String getLastDayBeginTime(int n){
        //获取时间
        Date data=new Date();
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(data);
        calendar.add(calendar.DATE,n);
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return sdf.format(calendar.getTime());
    }
    /**获取过去的结束时间*/
    public String getLastDayEndTime(int n){
        //获取时间
        Date data=new Date();
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(data);
        calendar.add(calendar.DATE,n);
        calendar.set(Calendar.HOUR, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return sdf.format(calendar.getTime());
    }

    public  String getMonthBeginTime(int year, int month) {
         month=month-1;
        mCal.set(Calendar.YEAR,year);
        mCal.set(Calendar.MONTH,month);
        mCal.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        return format.format(mCal.getTime());
    }

    public  String getMonthEndTime(int year, int month) {
        month=month-1;
        mCal.set(Calendar.YEAR,year);
        mCal.set(Calendar.MONTH,month);
        mCal.set(Calendar.DAY_OF_MONTH, mCal.getActualMaximum(Calendar.DAY_OF_MONTH));
        return format.format(mCal.getTime());
    }
}
